package com.dodock.banglapapers.server.helper;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsListAmarDesh implements DDNNewsListInterface {

	private String BASE_URL = "http://www.kalerkantho.com/print_edition/index.php?view=details&type=main&cat_id=%s&menu_id=%s";
	private String IMAGE_BASE = "http://www.kalerkantho.com/print_edition/";

	@Override
	public String GenerateNewsList(String categoryid) {
		try {
			String url = "";
			if (categoryid.equalsIgnoreCase("81")
					|| categoryid.equalsIgnoreCase("214")) {
				url = String.format(BASE_URL, "3", categoryid);
			} else {
				url = String.format(BASE_URL, "1", categoryid);
			}
			if (categoryid.equalsIgnoreCase("92")
					|| categoryid.equalsIgnoreCase("81")) {
				url = url + "&pub_no=1280";
			}
			return getNewsList(url);
		} catch (Exception ex) {
			System.out.print("exeception : " + ex.toString());
			return "[]";
		}
	}
	private void parseNewsObject(JsonArray array, Element element) {
		Elements links;
		links = element
				.select("span[style=display:block; text-align:left; font-size:22px]");
		if (links.isEmpty()) {
			links = element
					.select("a[style=text-decoration:none; color:#005689; cursor:pointer]");
		} else {
			links = links
					.select("a[style=text-decoration:none; color:#CD0000; cursor:pointer]");
		}
		Element aLink = links.first();
		if (aLink != null) {
			JsonObject obj = new JsonObject();
			String text = aLink.text();
			String id = aLink.attr("href").trim();
			id = id.substring(id.lastIndexOf("pub_no=")).replaceAll("&", "%26");
			obj.addProperty("id", id);
			obj.addProperty("title", text);
			Elements imgObj = element.select("a[rel=prettyPhoto]");
			if (!imgObj.isEmpty()) {
				String img = imgObj.first().attr("href").trim();
				obj.addProperty("image", IMAGE_BASE + img);
			}
			array.add(obj);
		}
	}

	private void parseExtraNews(JsonArray array, Element element) {
		String text = "";
		String id = "";
		Element as = element.select("a").first();
		if (as != null) {
			Elements a = as.select("span[style=display:block; font-size:14px]");
			if (!a.isEmpty()) {
				if (a.first() != null) {
					text = a.first().text();
				} else
					return;
			}
			if (text.length() <= 1)
				return;

			JsonObject obj = new JsonObject();
			id = as.attr("href").trim();
			id = id.substring(id.lastIndexOf("pub_no=")).replaceAll("&", "%26");
			obj.addProperty("id", id);
			obj.addProperty("title", text);
			array.add(obj);
		}
	}

	private String getNewsList(String url) {
		// TODO Auto-generated method stub
		JsonArray array = new JsonArray();
		Document doc;
		try {
			Connection.Response response = Jsoup
					.connect(url)
					.userAgent(
							"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
					.timeout(10000).execute();
			doc = response.parse();
			Elements mastHeads = null;
			mastHeads = doc
					.select("div[style=border-top:1px dotted #cccccc; padding-bottom:10px]");
			for (Element element : mastHeads) {
				parseNewsObject(array, element);
			}

			Elements extraNews = doc.select("div[class=alink]");
			if (!extraNews.isEmpty()) {
				for (Element news : extraNews) {
					parseExtraNews(array, news);
				}
			}
			return array.toString();
		} catch (Exception e) {
			System.out.println(e.toString());
			return "[]";
		}
	}
}
